package cz.schovjan.mte_proj.task;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import cz.schovjan.mte_proj.Constant;
import cz.schovjan.mte_proj.R;
import cz.schovjan.mte_proj.dto.LyricResult;
import cz.schovjan.mte_proj.fragment.SearchByTextFragment;
import cz.schovjan.mte_proj.service.Searcher;
import cz.schovjan.mte_proj.service.SearcherImpl;

/**
 * Dotahuje text pisne 
 * @author schovjan
 *
 */
public class DownloadLyricTask extends AsyncTask<LyricResult, Void, LyricResult> {

    private ProgressDialog waiter = null;
    private Searcher searcher = null;
    private SearchByTextFragment activity = null;


    public DownloadLyricTask(SearchByTextFragment a) {
	this.activity = a;
	searcher = new SearcherImpl();
    }


    @Override
    protected LyricResult doInBackground(LyricResult... params) {
	LyricResult lr = params[0];
	try {
	    Log.i(Constant.TAG, "spoustim dotazeni textu");
	    searcher.loadLyric(lr);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return lr;
    }


    @Override
    protected void onPostExecute(LyricResult result) {
	if (waiter != null) {
	    waiter.dismiss();
	}
	((SearchByTextFragment)activity).startLyricAktivity(result);
    }


    @Override
    protected void onPreExecute() {
	waiter = ProgressDialog.show(activity.getActivity(), activity.getActivity().getResources().getString(R.string.wait_please), activity
		.getResources().getString(R.string.loading_text));
    }
}
